package com.woniuxy.microretailserver.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microretailserver.bean.GoodsOrderBean;
import com.woniuxy.microretailserver.bean.GoodsOrderPayExportBean;
import com.woniuxy.microretailserver.bean.PageRequest;
import com.woniuxy.microretailserver.service.IGoodsOrderService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author chenhu
 * 商城销售单控制层
 */
@RestController
@RequestMapping("/mallSales")
public class MallSalesController {

    @Resource
    private IGoodsOrderService goodsOrderServiceImpl;

    /**
     * 展示商城销售单数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/find",produces = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<GoodsOrderBean> findGoodsOrderPay(PageRequest request){
        GoodsOrderBean goodsOrderBean = new GoodsOrderBean();
        PageInfo<GoodsOrderBean> allGoodsOrderPay = goodsOrderServiceImpl.findAllGoodsOrderPay(request, goodsOrderBean);
        return allGoodsOrderPay;
    }

    /**
     * 导出商城销售单数据
     * @throws Exception
     */
    @GetMapping(value = "/work",produces = MediaType.APPLICATION_JSON_VALUE)
    public void findByAllGoodsPay() throws Exception{
        List<GoodsOrderPayExportBean> byAllGoodsPay = goodsOrderServiceImpl.findByAllGoodsPay();
        byAllGoodsPay.forEach(e->{
            System.out.println(e);
        });
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("销售表", "销售数据"), GoodsOrderPayExportBean.class, byAllGoodsPay);
        FileOutputStream outputStream = new FileOutputStream("C:/Users/Administrator/Desktop/goodsOrderPay.xls");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }

    /**
     * 模糊查询销售单数据
     * @param request
     * @param goodsOrderBean
     * @return
     */
    @GetMapping(value = "/module",produces = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<GoodsOrderBean> findModule(PageRequest request,GoodsOrderBean goodsOrderBean){
        PageInfo<GoodsOrderBean> allGoodsOrderPay = goodsOrderServiceImpl.findAllGoodsOrderPay(request, goodsOrderBean);
        return allGoodsOrderPay;
    }
}
